import { Base } from '../base.js';

class Auth extends Base {
	
	//验证token
	verify(refereeId) {
		var token = uni.getStorageSync('Access-Token-UNI');
		if (!token) {
			this.getTokenFromServer(refereeId);
		} else {
			this.veirfyFromServer(token, refereeId);
		}
	}
	// 携带令牌去服务器校验令牌
	veirfyFromServer(token, refereeId) {
		let that = this;
		var params = {
			url: 'v1/auth/verifyToken',
			type: 'post',
			data: {
				token: token
			}
		}
		that.request(params).then(res => {
			//不存在，则从服务器重新获取
			if(res.code == 0){
				that.getTokenFromServer(refereeId);
			}
		}).catch(err => {
			console.log(err)
		});
	}
	//从服务器获取token
	getTokenFromServer(refereeId) {
		var that = this;
		console.log('getTokenFromServer                    getTokenFromServer')
		uni.login({
			success: function(res) {
				var params = {
					url: 'v1/auth/getToken',
					type: 'post',
					data: {
						form: {
							code: res.code,
							refereeId: refereeId
						}
					}
				}
				
				that.request(params).then(resu => {
					if(resu.code == 1){
						uni.setStorageSync('Access-Token-UNI', resu.data.token)
						uni.setStorageSync('userInfo', resu.data.userInfo)
					}
				}).catch(err => {
					console.log(err)
				});
				
			}
		})
	}

};

export { Auth };

class Notice extends Base {
	
};

export { Notice };